/**
* @description: 978. 最长湍流子数组
* @author hewei
* @date 2022/9/27 16:12
* @version 1.0
*/

public class MaxTurbulenceSize {

    public static void main(String[] args) {
        int[] nums = {9,4,2,10,7,8,8,1,9};
        System.out.println(new MaxTurbulenceSize().maxTurbulenceSize(nums));
    }

    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        int l = 0;
        int r = 0;
        int ans = 1;
        int last = -2;
        while (r < n - 1) {
            int temp = Integer.compare(arr[r + 1], arr[r]);
            if (last == -temp || (last == -2 && temp != 0)) {
                last = temp;
                ++r;
                ans = Math.max(ans, r - l + 1);
            } else {
                if (l == r) ++r;
                l = r;
                last = -2;
            }
        }
        return ans;
    }
}
